package br.com.projeto;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import br.com.clientes.R;
import br.com.projeto.DAO.ClienteDAO;
import br.com.projeto.VO.ClienteVO;

public class Cadastro extends Activity {

	private EditText txtNome;
	private EditText txtEmail;
	private EditText txtEndereco;
	private EditText txtTelefone;
	private EditText txtCor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cadastro);
		Button btnCadastro = (Button) findViewById(R.id.btCCadastrar);

		Button btnCor = (Button) findViewById(R.id.btCor);
		txtNome = (EditText) findViewById(R.id.txtListarNome);
		txtEmail = (EditText) findViewById(R.id.txtEmail);
		txtEndereco = (EditText) findViewById(R.id.txtEndereco);
		txtTelefone = (EditText) findViewById(R.id.txtTelefone);
		txtCor = (EditText) findViewById(R.id.txtCor);

		btnCadastro.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				ClienteVO cliente = new ClienteVO();
				cliente.setNome(txtNome.getText().toString());
				cliente.setEmail(txtEmail.getText().toString());
				cliente.setEndereco(txtEndereco.getText().toString());
				cliente.setTelefone(txtTelefone.getText().toString());

				ClienteDAO dao = new ClienteDAO(getBaseContext());
				if (dao.insert(cliente)) {
					Toast.makeText(getBaseContext(),
							"Cliente inserido com sucesso", Toast.LENGTH_SHORT)
							.show();
				}
			}
		});
		btnCor.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivityForResult(new Intent(getBaseContext(),
						ListarCores.class), 1);
			}

		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 1 && resultCode == RESULT_OK) {
			String cor = data.getStringExtra("cor");
			Toast.makeText(getBaseContext(), data.getStringExtra("cor"),
					Toast.LENGTH_SHORT).show();
			txtCor.setText(cor);
		}

	}
}
